package cn.lf.service.impl;
import java.math.BigDecimal;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.lf.constant.OrderStatusEnum;
import cn.lf.dto.order.OrderInfoAddDTO;
import cn.lf.dto.order.OrderInfoSetDTO;
import cn.lf.dto.sys.order.OrderInfoGetRepDTO;
import cn.lf.dto.sys.order.OrderInfoGetReqDTO;
import cn.lf.dto.sys.order.OrderInfoSingleDTO;
import cn.lf.dto.order.OrderStatusSetDTO;
import cn.lf.entity.OrderDetailInfo;
import cn.lf.entity.OrderInfo;
import cn.lf.entity.ShopInfo;
import cn.lf.entity.SysUser;
import cn.lf.mapper.OrderDetailInfoMapper;
import cn.lf.mapper.OrderInfoMapper;
import cn.lf.mapper.ShopInfoMapper;
import cn.lf.mapper.SysUserMapper;
import cn.lf.service.IOrderInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guoyy
 * @since 2024-04-07
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private ShopInfoMapper shopInfoMapper;

    @Resource
    private OrderDetailInfoMapper orderDetailInfoMapper;

    @Override
    public IPage<OrderInfoGetRepDTO> queryOrderInfoPage(OrderInfoGetReqDTO params) {

        Page<OrderInfoGetRepDTO> page = new Page(params.getCurrentPage(),params.getPageSize());

        IPage<OrderInfoGetRepDTO> result = baseMapper.selectOrderInfoPage(page,params);

        for (OrderInfoGetRepDTO record : result.getRecords()) {

            record.setProductList(orderDetailInfoMapper.selectOrderProductInfo(record.getOrderId()));
            
        }

        return result;
    }

    @Override
    public void setOrderStatus(OrderStatusSetDTO orderStatusSetDTO) {

        LambdaUpdateWrapper<OrderInfo> wrapper = new LambdaUpdateWrapper<>();

        wrapper.set(OrderInfo::getOrderStatus,orderStatusSetDTO.getOrderStatus());

        if (Objects.equals(orderStatusSetDTO.getOrderStatus(),OrderStatusEnum.REFUND.getCode())){

            wrapper.set(OrderInfo::getRefundPay,orderStatusSetDTO.getRefundPay());

        }

        wrapper.eq(OrderInfo::getOrderId,orderStatusSetDTO.getOrderId());

        baseMapper.update(null,wrapper);

    }

    @Override
    public OrderInfoSingleDTO getOrderInfoById(String orderId) {

        OrderInfoSingleDTO result = baseMapper.selectOrderInfoSingle(orderId);

        List<OrderInfoSingleDTO.ProductInfo> productList = orderDetailInfoMapper.selectOisProductList(orderId);

        result.setProductList(productList);

        return result;
    }

    @Override
    public void setOrderInfo(OrderInfoSetDTO orderInfoSetDTO) {
        LambdaUpdateWrapper<OrderInfo> wrapper = new LambdaUpdateWrapper<>();

        wrapper.set(OrderInfo::getBuyerMessage,orderInfoSetDTO.getBuyerMessage());

        wrapper.set(OrderInfo::getPaymentType,orderInfoSetDTO.getPaymentType());

        wrapper.set(OrderInfo::getBuyerRate,orderInfoSetDTO.getBuyerRate());

        wrapper.set(OrderInfo::getInvoiceType,orderInfoSetDTO.getInvoiceType());

        wrapper.eq(OrderInfo::getOrderId,orderInfoSetDTO.getOrderId());

        baseMapper.update(null,wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delOrderInfoById(String orderId) {

        baseMapper.deleteById(orderId);

        LambdaQueryWrapper<OrderDetailInfo> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(OrderDetailInfo::getOrderId,orderId);

        orderDetailInfoMapper.delete(wrapper);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrderInfo(OrderInfoAddDTO orderInfoAddDTO) {

        OrderInfo orderInfo = new OrderInfo();

        BeanUtils.copyProperties(orderInfoAddDTO,orderInfo);

        orderInfo.setOrderId(UUID.randomUUID().toString());

        orderInfo.setOrderNumber("123");

        orderInfo.setOrderStatus(OrderStatusEnum.UN_PAID.getCode());

        orderInfo.setCreateTime(new Date());

        orderInfo.setBuyerRate("0");

        orderInfo.setSourceType("1");

        baseMapper.insert(orderInfo);

        OrderDetailInfo orderDetailInfo;

        for (OrderInfoAddDTO.productInfo productInfo : orderInfoAddDTO.getProductInfoList()) {

            orderDetailInfo = new OrderDetailInfo();

            orderDetailInfo.setOrderDetailId(UUID.randomUUID().toString());
            orderDetailInfo.setOrderId(orderInfo.getOrderId());
            orderDetailInfo.setProductId(productInfo.getProductId());
            orderDetailInfo.setProductNumber(productInfo.getProductNumber());
            orderDetailInfo.setProductPrice(BigDecimal.valueOf(productInfo.getProductPrice()));

            orderDetailInfoMapper.insert(orderDetailInfo);
        }

    }
}
